package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSeetingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSeetingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            //对集合进行遍历，将每个对象保存至数据库
            for (OrderSetting orderSetting : list) {
                //检查此数据是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //代表数据已存在
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSetttingByMonth(String date) {
        String actionDate = date + "-1";
        String lastDate = date + "-31";
        HashMap<String, String> map1 = new HashMap<>();
        /*map1.put("actionDate",actionDate);
        map1.put("lastDate",lastDate);
        orderSettingDao.getOrderSettingByMonth(map1);
        参数为Map集合*/
        //获取该月份所有的预约详情
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(actionDate, lastDate);
        ArrayList<Map> maps = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map map = new HashMap();
            map.put("date", orderSetting.getOrderDate().getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            maps.add(map);
        }
        return maps;
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            //代表已存在进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //进行添加操作
            orderSettingDao.add(orderSetting);
        }

        orderSettingDao.editNumberByOrderDate(orderSetting);
    }
}
